﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Nodes;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Util;

namespace WBInfo
{
    public partial class Form1 : Form
    {

        private bool openFlag = true;
        private DateTime stopDate =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 15:10:00");
        private DateTime beginDate =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 09:00:00");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initPro();
            updateSearchNow();
            updateJJdata();
            updateFunddata();
        }

        private void initPro()
        {
            ShowInTaskbar = false;//取消在任务栏显示
            BackColor = Color.White;
            TransparencyKey = Color.White;
            FormBorderStyle = FormBorderStyle.None; //取消Form标题栏    
            Height = Screen.PrimaryScreen.WorkingArea.Height;
            Width = Screen.PrimaryScreen.WorkingArea.Width / 2;
            int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
            //计算窗体显示的坐标值，可以根据需要微调几个像素
            int formX = ScreenWidth - this.Width;
            int formY = 0;
            this.Location = new Point(formX, formY); //布置空间位置

            int groupWbx = this.Width - groupBox1.Width - 10;
            int groupWby = formY + 10;
            groupBox1.Location = new Point(groupWbx, groupWby);//热搜榜位置
            
            int groupJJx = groupWbx - groupBox2.Width - 10;
            int groupJJy = groupWby;
            groupBox2.Location = new Point(groupJJx, groupJJy);//经济栏位置
            
            int groupFundx = groupJJx;
            int groupFundy = groupJJy + groupBox2.Height + 10 ;
            groupBox3.Location = new Point(groupFundx, groupFundy);//基金栏位置
            
            //初始化右小角图标
            this.notifyIcon1.Visible=true;//在通知区显示Form的Icon
            this.notifyIcon1.ContextMenuStrip = iconClick;
            this.WindowState=FormWindowState.Normal;
      
            
            iconClick.Items[0].Click += new EventHandler (closePro);
            iconClick.Items[1].Click += new EventHandler (movePro);
            iconClick.Items[2].Click += new EventHandler (openFundFile);
        }
        
        private void updateSearchNow()
        {
            Dictionary<string, object> data = WBInfoAction.getSearchNowData();
            if (data["result"].Equals("success"))
            {
                List<string> searchData = (List<string>) data["data"];
                linkLabel1.Text = "*." + searchData[0];
                linkLabel1.Name = searchData[0];
                linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel2.Text = "1." + searchData[1];
                linkLabel2.Name = searchData[1];
                linkLabel2.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel3.Text = "2." + searchData[2];
                linkLabel3.Name = searchData[2];
                linkLabel3.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel4.Text = "3." + searchData[3];
                linkLabel4.Name = searchData[3];
                linkLabel4.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel5.Text = "4." + searchData[4];
                linkLabel5.Name = searchData[4];
                linkLabel5.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel6.Text = "5." + searchData[5];
                linkLabel6.Name = searchData[5];
                linkLabel6.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel7.Text = "6." + searchData[6];
                linkLabel7.Name = searchData[6];
                linkLabel7.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel8.Text = "7." + searchData[7];
                linkLabel8.Name = searchData[7];
                linkLabel8.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel9.Text = "8." + searchData[8];
                linkLabel9.Name = searchData[8];
                linkLabel9.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel10.Text = "9." + searchData[9];
                linkLabel10.Name = searchData[9];
                linkLabel10.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel11.Text = "10." + searchData[10];
                linkLabel11.Name = searchData[10];
                linkLabel11.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel12.Text = "11." + searchData[11];
                linkLabel12.Name = searchData[11];
                linkLabel12.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel13.Text = "12." + searchData[12];
                linkLabel13.Name = searchData[12];
                linkLabel13.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel14.Text = "13." + searchData[13];
                linkLabel14.Name = searchData[13];
                linkLabel14.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel15.Text = "14." + searchData[14];
                linkLabel15.Name = searchData[14];
                linkLabel15.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);
                
                linkLabel16.Text = "15." + searchData[15];
                linkLabel16.Name = searchData[15];
                linkLabel16.LinkClicked += new LinkLabelLinkClickedEventHandler (lbllink_LinkClicked1);

                
            }else {
                
            }
            groupBox1.Text = "微博实时热搜-" + DateTime.Now.ToShortTimeString().ToString() + "更新";
        }

        private void updateJJdata()
        {
            Dictionary<string, object> data = JJInfoAction.getAGuNowData();
            if (data["result"].Equals("success"))
            {
                label1.Text = data["shName"] + ":" + data["shIncrePer"] + "%";
                changeLabelColor(label1,(string)data["shIncrePer"]);
                label2.Text = data["szName"] + ":" + data["szIncrePer"] + "%";
                changeLabelColor(label2,(string)data["szIncrePer"]);
                
            }else {
                
            }
            groupBox2.Text = "A股最新涨跌幅-" + DateTime.Now.ToShortTimeString().ToString() + "更新";
        }

        private void updateFunddata()
        {
            groupBox3.Controls.Clear();
            Dictionary<string, object> fundCode = FileAction.checkFundFile();
            
            if (fundCode["result"].Equals("success"))
            {
                int count = (int)fundCode["count"];
                if (count == 0)
                {
                    Label fundNodatLabel = new Label();
                    initLabel(fundNodatLabel,"fundNodatLabel","请在图标栏添加基金代码",6,30,1);
                    groupBox3.Controls.Add(fundNodatLabel);
                    groupBox3.Height = 50;
                    groupBox3.Text = "基金实时涨幅";
                }
                else
                {
                    string[] fundCodeData = (string[]) fundCode["data"];
                    for (int i = 0; i < count; i++)
                    {
                        Dictionary<string, object> data = JJInfoAction.getFundNowData(Regex.Replace(fundCodeData[i], @"[^0-9]+", ""));
                        Label fundLabel = new Label();
                        string content;
                        if (data["result"].Equals("success"))
                        {
                            if (i == 0)
                            {
                                groupBox3.Text = "基金实时涨幅-" + data["gztime"] + "更新";
                            }
                            string name = (string) data["name"];
                            name = name.Length > 10 ? name.Substring(0, 10) : name;
                            content = name + ":" + data["gszzl"] + "%";
                            changeLabelColor(fundLabel,(string)data["gszzl"]);
                        }
                        else
                        {
                            groupBox3.Text = "请检查网络连接";
                            content = "数据正在加载中...";
                        }
                        initLabel(fundLabel,"fundLabel"+(i+1),content,6,(i+1) * 30,i+1);
                        groupBox3.Controls.Add(fundLabel);
                    }
                    groupBox3.Height = count * 30 + 25;
                }
            }else if (fundCode["result"].Equals("No data"))
            {
                Label fundNodatLabel = new Label();
                initLabel(fundNodatLabel,"fundNodatLabel","请在右下角图标栏添加基金代码",6,40,1);
                groupBox3.Controls.Add(fundNodatLabel);
                groupBox3.Height = 50;
                groupBox3.Text = "基金实时涨幅";
            } else{
                
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            updateSearchNow();
            if (DateTime.Now <= stopDate && DateTime.Now >= beginDate)
            {
                updateJJdata();
                updateFunddata();
            }
        }

        private void closePro(object o, EventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }
        
        private void movePro(object o, EventArgs e)
        {
            if (this.FormBorderStyle == FormBorderStyle.None){
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
            else{
                this.FormBorderStyle = FormBorderStyle.None;
            }
        }

        private void openFundFile(object o, EventArgs e)
        {
            string filePath = Environment.CurrentDirectory + "\\" + FileAction.fundCodeFileName;
            System.Diagnostics.Process.Start(filePath);
        }
        

        private void lbllink_LinkClicked1(object o, LinkLabelLinkClickedEventArgs e)
        {
            if (openFlag)
            {
                openFlag = false;
                LinkLabel label = (LinkLabel) o;
                Process.Start("https://s.weibo.com/weibo?q="+label.Name+"&Refer=top");
                timer2.Enabled = true;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            openFlag = true;
            timer2.Enabled = false;
        }

        private void changeLabelColor(Label label,string num)
        {
            double num1 = double.Parse(num);
            if (double.Parse(num) > 0)
            {
                label.ForeColor = Color.Red;
            }
            else if(double.Parse(num) < 0)
            {
                label.ForeColor = Color.Green;
            }
            else
            {
                label.ForeColor = Color.Black;
            }
        }

        private void initLabel(Label label,string labelName,string content,int x,int y,int index)
        {
            label.Location = new System.Drawing.Point(x, y);
            label.Name = labelName;
            label.Size = new System.Drawing.Size(188, 23);
            label.TabIndex = index;
            label.Text = content.Equals("")?"正在加载中......\r\n":content;
        }
        
    }
    
}

